<?php
/**
*	reim
*/
namespace Rainrock\Framework\Chajian;


use Rainrock\Framework\kernel\core\Chajian;
use Rainrock\Framework\kernel\core\Module;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Jiami;
use Rainrock\Framework\kernel\base\Rock;

class reimChajian extends Chajian{
	
	protected $server_push  = '';
	protected $server_recid = '';
	
	//初始化配置读取
	public function initChajian()
	{
		$purl 	 = Base::getConfig('server_push');
		if($this->isempt($purl))$purl = 'http://127.0.0.1:6553/';
		$this->server_push	  = $purl;
		$this->server_recid	  = Base::getConfig('server_recid');
	}
	
	/**
	*	发送队列信息
	*	$cont 内容可以是http地址，也可以如:cli,run
	*	$param 参数
	*	使用 $this->c('reim')->push('cli,run');
	*/
	public function push($cont, $param=array(), $runtime=0, $name='', $logarr=array())
	{
		$type 	= 'cmd';
		$url	= $cont;
		//$param['cfromurl'] = Jiami::base64encode(Rock::url());
		$param['addlog']    = 'true';
		$mode 	= Module::get('sysqueue','system');
		if(substr($cont,0,4)=='http'){
			$type='url';
		}else{
			if(!$this->contain($url, ','))$url='cli,'.$url.'';
			$phppath = Base::getConfig('phppath');
			if(!$this->contain($this->server_push, '127.0.0.1') || $this->isempt($phppath)){
				$urla= explode(',', $url);
				$url = URL.'index.php?m='.$urla[0].'&a='.$urla[1].'';
				$type= 'url';
			}else{
				$st1 = '';
				foreach($param as $k=>$v)$st1.=' '.$k.'='.$v.'';
				$url = ''.$phppath.' '.ROOT_PATH.'/index.php '.str_replace(',','/',$url).''.$st1.'';
			}
		}
		if($type=='url'){
			$jg  = $this->contain($url,'?')?'&':'?';
			$st1 = '';
			foreach($param as $k=>$v)$st1.='&'.$k.'='.$v.'';
			if($st1!='')$url.=''.$jg.''.substr($st1,1).'';
		}
		if(!$name)$name   = '异步队列';
		$yundt 			  = date('Y-m-d H:i:s', (($runtime==0) ? time() : $runtime));
		$logarr['result'] = '待运行('.$yundt.')';
		if(!isset($logarr['optname']))$logarr['optname']='系统';
		$id 	= $this->m('syslog')->addQueue($name, $url, $logarr);
		if($id){
			if($type=='cmd'){
				$url .= ' syslogid='.$id.'';
			}else{
				$url .= '&syslogid='.$id.'';
			}
		}
		if(!$id)$id = rand(1,99999);
		$rarr[] = array(
			'qtype'		=> $type,
			'runtime'	=> $runtime,
			'url'		=> $url,
			'id'		=> $id
		);
		$barr = $this->pushdata($rarr);
		$barr['cmdurl'] = ''.$type.':'.$url;
		return $barr;
	}
	
	/**
	*	推送数据过去
	*/
	public function pushdata($rarr)
	{
		if(!$rarr)return $this->returneorror('发空内容');
		if(!$this->server_push)return $this->returnerror('没有配置服务端');
		if(is_array($rarr))$rarr = json_encode($rarr);
		$url = ''.$this->server_push.'?atype=send&data='.urlencode($rarr).'';
		$reqult = $this->c('curl')->setTimeout(5)->getcurl($url);
		if($reqult){
			return $this->returnsuccess($reqult);
		}else{
			return $this->returnerror('服务端配置不能用');
		}
	}
	
	/**
	*	向客户端推送消息
	*/
	public function pushclient($receid, $cans=array())
	{
		$carr['from'] 	= $this->server_recid;
		$carr['adminid']= 0;
		$carr['atype'] 	= 'send'; //也是固定的
		$carr['qtype'] 	= 'reim'; //固定说明是聊天消息
		$carr['receid'] = $receid; 
		foreach($cans as $k=>$v)$carr[$k]=$v;
		return $this->pushdata($carr);
	}
}